module.exports = function(grunt) {
    var fs = require('fs');
    var path = require('path');

    grunt.loadNpmTasks('grunt-contrib-requirejs');
    grunt.loadNpmTasks('grunt-contrib-jshint');


    var project = grunt.option("p");
    if (!project) {
        console.error("please use -p=prjectName mode to build your project");
        return;
    }
    var projectPath = "./Apps/" + project;//path.resolve("./Apps/",project);

    //打包文件
    var main = ["./js/main"];
    try {
        //打包项目下的组件
        var customerDir = projectPath + "/components";//path.resolve();
        var customerComponents = fs.readdirSync(customerDir);
        for (var i = 0, j = customerComponents.length; i < j; i++) {
            var name = customerComponents[i].split(".js")[0];
            customerComponents[i] = customerDir + "/" + name;
        }
        main = main.concat(customerComponents);

        //打包项目下的libs
        var libdir = projectPath + "/libs";
        var libs = fs.readdirSync(libdir);
        for (var i = 0, j = libs.length; i < j; i++) {
            var name = libs[i].split(".js")[0];
            libs[i] = libdir + "/" + name;
        }
        main = main.concat(libs);
    } catch (e) {
        console.log(e);
    }

    //打包项目下的页面文件
    dir = projectPath + "/pages";
    var pages = fs.readdirSync(dir);
    for (var i = 0, j = pages.length; i < j; i++) {
        var name = pages[i].split(".js")[0];
        pages[i] = dir + "/" + name;
    }
    main = main.concat(pages);

    // 构建controller路径，这里由于controller路径分两种，一种是logic，一种是controller。
    var projectPages = projectPath + "/pages/**.js";
    var controller = projectPath + "/logic";
    if(!fs.existsSync(controller)){
        controller = projectPath + "/controllers";
    }
    controller = controller + "/**.js";
    var distOutPath = "Apps/" + project;
    //路径
    var outPath = distOutPath + "/dist";
    //输出mainjs路径
    var mainPath = outPath + "/js/main.min.js";

    //css压缩压缩路径
    var cssDistPath = outPath + "/css/common.min.css";
    var projectCssPath = distOutPath + "/css/**.css";

    //字体
    var fontPath = "./fonts/*";
    var distFontPath = outPath + "/";
    var projectFontPath = projectPath + "/fonts/";
    var projectDistFontPath = outPath + "/fonts/";

    //图片
    var images = projectPath + "/imgs";
    var distImages = outPath + "/imgs/";

    var appPath = projectPath + "/app";

    //grunt 配置
    var config = {
        pkg: grunt.file.readJSON('package.json'),
        jshint: {
            all: [projectPages, controller, "./js/**.js"]
        },
        requirejs: {
            compile: {
                "options": {
                    "baseUrl": "./",
                    "paths": {
                        "app":appPath,
                        "jquery": "./libs/jquery",
                        "pm": "./js/pageViewManager",
                        "base": "./components/base",
                        "dialog": "./components/dialog",
                        "pageview": "./components/pageview",
                        "utils": "./js/utils",
                        "spin": "./libs/spin",
                        "spinjquery": "./libs/spin.jquery",
                        "polyfill": "./libs/polyfill"
                    },
                    "include": main,
                    "out": mainPath
                }
            },
        },
        copy: {
            main: {
                files: [
                    {
                        src: [fontPath],
                        dest: distFontPath
                    },
                    {
                        expand: true,
                        cwd: projectFontPath,//项目字体文件
                        src: '**',//所有当前目录的文件
                        dest: projectDistFontPath,//复制到目标目录
                        flatten: true,
                        filter: 'isFile'//仅文件
                    },
                    {
                        expand: true,
                        cwd: images,//项目图片资源
                        src: '**',//所有当前目录的文件
                        dest: distImages,//复制到目标目录
                        flatten: true,
                        filter: 'isFile'//仅文件
                    },
                ],
            },
        },
        cssmin: {
            compress: {}
        },
    };

    //这里必须要动态配置
    config.cssmin.compress.files = {};
    config.cssmin.compress.files[cssDistPath] =  [
        "./css/**.css",
        projectCssPath
    ];
    grunt.initConfig(config);


    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    // 默认被执行的任务列表。
    grunt.registerTask('build', ["jshint", 'requirejs', 'cssmin', 'copy']);

};
